//============================================================================
// Copyright (C) 2013 Brett R. Jones
//
// Issued to public domain 2013
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================
#include "VxConnectInfo.h"
#include "VxParse.h"
#include "VxSktUtil.h"

#define ONLINE_STATUS_CONNECTED				0x01
#define ONLINE_STATUS_NEARBY				0x02
#define ONLINE_STATUS_FROM_SEARCH_PKT		0x04 // is from search list.. never connected to


//////////////////////////////////////////////////////////////////////////////
//=== constructor ==//
VxConnectInfo::VxConnectInfo()
	: m_u8OnlineStatusFlags(0)
{
}

void			VxConnectInfo::setMyOnlineId( U64 u64HiPart, U64 u64LoPart ){ m_oDirectConnectId.m_u64IdHiPart = u64HiPart; m_oDirectConnectId.m_u64IdLoPart = u64LoPart; }
OnlineId&		VxConnectInfo::getMyOnlineId()								{ return m_oDirectConnectId; }
void			VxConnectInfo::getMyOnlineId( std::string& strRetId )		{ return m_oDirectConnectId.toOnlineIdHexString( strRetId ); }
U64				VxConnectInfo::getMyOnlineIdLoPart()						{ return m_oDirectConnectId.getOnlineIdLoPart(); }
U64				VxConnectInfo::getMyOnlineIdHiPart()						{ return m_oDirectConnectId.getOnlineIdHiPart(); }

OnlineId&		VxConnectInfo::getRelayOnlineId()							{ return m_oRelayConnectId; }
void			VxConnectInfo::getRelayOnlineId( std::string& strRetId )	{ return m_oRelayConnectId.toOnlineIdHexString( strRetId ); }
U64				VxConnectInfo::getRelayOnlineIdLoPart()						{ return m_oRelayConnectId.getOnlineIdLoPart(); }
U64				VxConnectInfo::getRelayOnlineIdHiPart()						{ return m_oRelayConnectId.getOnlineIdHiPart(); }
U16				VxConnectInfo::getRelayPort( void )							{ return m_oRelayConnectId.m_u16OnlinePort; }

//! get online ip address in dotted format
void			VxConnectInfo::getMyOnlineIPv4( std::string& strRetIp )		{ if( m_oUdpIp.isValid() ) m_oUdpIp.toStdString( strRetIp ); m_oDirectConnectId.m_oIPv4OnlineIp.toStdString( strRetIp ); }
void			VxConnectInfo::getMyOnlineIPv6( std::string& strRetIp )		{ return m_oDirectConnectId.getIPv6( strRetIp );}
VxIPv4Address	VxConnectInfo::getMyOnlineIPv4( void )						{ return m_oDirectConnectId.m_oIPv4OnlineIp; }
VxInetAddress	VxConnectInfo::getMyOnlineIPv6( void )						{ return m_oDirectConnectId.m_oIPv6OnlineIp;}

//! get ip based on if we can connect ipv6
VxInetAddress	VxConnectInfo::getOnlineIpAddress( void )
{
	//if( VxCanConnectUsingIPv6() && m_oDirectConnectId.m_oIPv6OnlineIp.isValid() )
	//{
	//	return m_oDirectConnectId.m_oIPv6OnlineIp;
	//}
	//else
	//{
		VxInetAddress oAddr;
		oAddr.setIp( m_oDirectConnectId.m_oIPv4OnlineIp.getIPv4Address() );
		return oAddr;
//	}
}
//! get ip based on if we can connect ipv6
VxInetAddress	VxConnectInfo::getRelayIpAddress( void )
{
	//if( VxCanConnectUsingIPv6() && m_oDirectConnectId.m_oIPv6OnlineIp.isValid() )
	//{
	//	return m_oRelayConnectId.m_oIPv6OnlineIp;
	//}
	//else
	//{
		VxInetAddress oAddr;
		oAddr.setIp( m_oRelayConnectId.m_oIPv4OnlineIp.getIPv4Address() );
		return oAddr;
//	}
}

void	VxConnectInfo::setOnlineIpAddress( const char * pIp )			{ m_oDirectConnectId.setIpAddress( pIp ); };

void	VxConnectInfo::setOnlineIpAddress( VxInetAddress& oIp )			
{ 
	if( oIp.isIPv4() )
	{
		m_oDirectConnectId.m_oIPv4OnlineIp.setIp( oIp.getIPv4AddressInNetOrder() );
	}
	else
	{
		m_oDirectConnectId.m_oIPv6OnlineIp  = oIp;
	}
}
//! get online port
U16		VxConnectInfo::getOnlinePort( void )							{ return m_oDirectConnectId.m_u16OnlinePort; };
//! get IP port as string
void	VxConnectInfo::getOnlinePort( std::string& strRetPort )			{ StdStringFormat( strRetPort, "%d", m_oDirectConnectId.m_u16OnlinePort ); }
//! set online port
void	VxConnectInfo::setOnlinePort( U16 u16Port )						{ m_oDirectConnectId.m_u16OnlinePort = u16Port; };

void		VxConnectInfo::setIsOnline( bool bIsOnline )				{ if( bIsOnline )(m_u8OnlineStatusFlags |= ONLINE_STATUS_CONNECTED); else m_u8OnlineStatusFlags &= (~ONLINE_STATUS_CONNECTED); }
bool		VxConnectInfo::isOnline( void )								{ return (m_u8OnlineStatusFlags & ONLINE_STATUS_CONNECTED)?1:0; }
void		VxConnectInfo::setIsNearby( bool bIsNearby )				{ if( bIsNearby )(m_u8OnlineStatusFlags |= ONLINE_STATUS_NEARBY); else m_u8OnlineStatusFlags &= (~ONLINE_STATUS_NEARBY); }
bool		VxConnectInfo::isNearby( void )								{ return (m_u8OnlineStatusFlags & ONLINE_STATUS_NEARBY)?1:0; }

void		VxConnectInfo::setIsFromSearchPkt( bool bIsFromSearch )		{ if( bIsFromSearch )(m_u8OnlineStatusFlags |= ONLINE_STATUS_FROM_SEARCH_PKT); else m_u8OnlineStatusFlags &= (~ONLINE_STATUS_NEARBY); }
bool		VxConnectInfo::isFromSearchPkt( void )						{ return (m_u8OnlineStatusFlags & ONLINE_STATUS_FROM_SEARCH_PKT)?1:0; }

//============================================================================
bool		VxConnectInfo::hasValidRelay( void )
{
	if( 0 == m_oRelayConnectId.getOnlineIdHiPart() ||
		0 == m_oRelayConnectId.getOnlineIdLoPart() ||
		0 == m_oRelayConnectId.m_u16OnlinePort )
	{
		return false;
	}
	return true;
}
